package com.supermap.guangzhou.weifang.activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.supermap.guangzhou.weifang.R;
import com.supermap.guangzhou.weifang.entity.FangWuAnquanXinXi;
import com.supermap.guangzhou.weifang.utils.NetUtils;
import com.supermap.guangzhou.weifang.utils.TimeUtils;
import com.supermap.guangzhou.weifang.utils.ToastUtil;
import com.supermap.guangzhou.weifang.utils.Utils;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.StringRequest;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnFocusChange;

public class PuchaLuruActivity extends AppCompatActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_dcbbh)
    EditText etDcbbh;
    @BindView(R.id.et_fwbsm)
    EditText etFwbsm;
    @BindView(R.id.et_fwjg)
    EditText etFwjg;
    @BindView(R.id.et_zdmj)
    EditText etZdmj;
    @BindView(R.id.et_jzmj)
    EditText etJzmj;
    @BindView(R.id.et_sjcs)
    EditText etSjcs;
    @BindView(R.id.et_fwdz)
    EditText etFwdz;
    @BindView(R.id.et_mph)
    EditText etMph;
    @BindView(R.id.et_xzq)
    EditText etXzq;
    @BindView(R.id.et_jd)
    EditText etJd;
    @BindView(R.id.et_hbbsm)
    EditText etHbbsm;
    @BindView(R.id.et_cfbsm)
    EditText etCfbsm;
    @BindView(R.id.tv_fwxz)
    EditText tvFwxz;
    @BindView(R.id.tv_fwcb)
    Spinner tvFwcb;
    @BindView(R.id.tv_fwzt)
    Spinner tvFwzt;
    @BindView(R.id.tv_fwjgxs)
    Spinner tvFwjgxs;
    @BindView(R.id.tv_jznd)
    Spinner tvJznd;
    @BindView(R.id.tv_cs)
    EditText tvCs;
    @BindView(R.id.tv_hs)
    Spinner tvHs;
    @BindView(R.id.tv_ywxfyh)
    Spinner tvYwxfyh;
    @BindView(R.id.tv_xfyhsm)
    EditText tvXfyhsm;
    @BindView(R.id.tv_ywbyyh)
    Spinner tvYwbyyh;
    @BindView(R.id.tv_byyhsm)
    EditText tvByyhsm;
    @BindView(R.id.tv_aqdj)
    Spinner tvAqdj;
    @BindView(R.id.cb_wxqk1)
    CheckBox cbWxqk1;
    @BindView(R.id.cb_wxqk2)
    CheckBox cbWxqk2;
    @BindView(R.id.cb_wxqk3)
    CheckBox cbWxqk3;
    @BindView(R.id.cb_wxqk4)
    CheckBox cbWxqk4;
    @BindView(R.id.cb_wxqk5)
    CheckBox cbWxqk5;
    @BindView(R.id.cb_wxqk6)
    CheckBox cbWxqk6;
    @BindView(R.id.tv_wxqk)
    EditText tvWxqk;
    @BindView(R.id.tv_xzqk)
    Spinner tvXzqk;
    @BindView(R.id.tv_hdnf)
    Spinner tvHdnf;
    @BindView(R.id.tv_sfjd)
    Spinner tvSfjd;
    @BindView(R.id.tv_jddw)
    EditText tvJddw;
    @BindView(R.id.tv_bgbh)
    EditText tvBgbh;
    @BindView(R.id.tv_jdsj)
    EditText tvJdsj;
    @BindView(R.id.tv_clyj)
    Spinner tvClyj;
    @BindView(R.id.tv_dcry)
    EditText tvDcry;
    @BindView(R.id.tv_dcrq)
    EditText tvDcrq;
    @BindView(R.id.btn_cancel)
    TextView btnCancel;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    private FangWuAnquanXinXi fangWuAnquanXinXi;

    private String insertSurveyUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pucha_luru);
        ButterKnife.bind(this);

        insertSurveyUrl = getResources().getString(R.string.insertSurvey);

        fangWuAnquanXinXi = getIntent().getParcelableExtra("bean");
        initWindow();
        initToolbar();

        initFangWuAnquanXinXi();


        tvJdsj.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });

    }

    private void initFangWuAnquanXinXi() {
        tvFwxz.setText(fangWuAnquanXinXi.getFwxz());
        etDcbbh.setText(fangWuAnquanXinXi.getDcbbh());
        etFwbsm.setText(fangWuAnquanXinXi.getFwbsm()+"");
        etJzmj.setText(fangWuAnquanXinXi.getYjzmj() + "");
        etZdmj.setText(fangWuAnquanXinXi.getYzdmj() + "");
        etSjcs.setText(fangWuAnquanXinXi.getSjcs() + "");
        etFwjg.setText(fangWuAnquanXinXi.getFwjg());
        etFwdz.setText(fangWuAnquanXinXi.getFwdz());
        etMph.setText(fangWuAnquanXinXi.getMph());
        etJd.setText(fangWuAnquanXinXi.getJqmc());
//        tvFwcb.setSelection(fangWuAnquanXinXi.getFwcb());
//        tvFwzt.setSelection(fangWuAnquanXinXi.getFwzt());
//        tvFwjgxs.setSelection(fangWuAnquanXinXi.getFwjg());
//        tvJznd.setSelection(fangWuAnquanXinXi.getJznd());
        tvCs.setText(fangWuAnquanXinXi.getSjcs() + "");
        tvHs.setSelection(fangWuAnquanXinXi.getHs());
        etXzq.setText(fangWuAnquanXinXi.getXzqmc());
//        tvYwxfyh.setSelection(fangWuAnquanXinXi.getYwxfyh());
//        tvYwbyyh.setSelection(fangWuAnquanXinXi.getYwbyyh());
//        tvAqdj.setSelection(fangWuAnquanXinXi.getAqdj());

    }


    public void initWindow() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = (int) (display.getHeight() * 0.8);
        params.width = (int) (display.getWidth() * 0.8);
        params.alpha = 1.0f;
        getWindow().setAttributes(params);
        getWindow().setGravity(Gravity.CENTER);
    }

    private void initToolbar() {
        tvTitle.setText("普查录入");
    }


    @OnClick({R.id.btn_back, R.id.btn_cancel, R.id.btn_confirm})
    public void onViewClicked(View view) {
        DatePickerDialog dialog = null;
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_confirm:
                StringRequest request = new StringRequest(insertSurveyUrl);
                if (fangWuAnquanXinXi != null) {
                    request.add("pczt", fangWuAnquanXinXi.getPczt());
                    request.add("dcbbh", fangWuAnquanXinXi.getDcbbh());
                    request.add("fwbsm", fangWuAnquanXinXi.getFwbsm());
                    request.add("yfwjg", fangWuAnquanXinXi.getYfwjg());
                    request.add("yzdmj", fangWuAnquanXinXi.getYzdmj());
                    request.add("yjzmj", fangWuAnquanXinXi.getYjzmj());
                    request.add("sjcs", fangWuAnquanXinXi.getSjcs());
                    request.add("fwdz", fangWuAnquanXinXi.getFwdz());
                    request.add("mph", fangWuAnquanXinXi.getMph());
                    request.add("xzqmc", fangWuAnquanXinXi.getXzqmc());
                    request.add("jqmc", fangWuAnquanXinXi.getJqmc());
                    request.add("fwxz", fangWuAnquanXinXi.getFwxz());
                    request.add("fwcb", fangWuAnquanXinXi.getFwcb());
                    request.add("fwzt", fangWuAnquanXinXi.getFwzt());
                    request.add("jznd", fangWuAnquanXinXi.getJznd());
                    request.add("sfjd", fangWuAnquanXinXi.getSfjd());
                    request.add("aqdj", fangWuAnquanXinXi.getAqdj());
                    request.add("wxqk", fangWuAnquanXinXi.getWxqk());
                    request.add("clyj", fangWuAnquanXinXi.getClyj());

//                    request.add("ywdt",fangWuAnquanXinXi.getYwdt());
                }
                new NetUtils<Void>(request) {

                    @Override
                    public Void onSuccess(Response<String> response) {
                        boolean succeed = response.isSucceed();
                        if (succeed) {
                            ToastUtil.makeText(Utils.getContext(),"操作成功!");
                            finish();
                        }
                        return null;
                    }

                    @Override
                    public Void onFail(Response<String> response) {
                        ToastUtil.makeText(Utils.getContext(), "联网失败了...");
                        return null;
                    }
                };

                break;
            default:
                break;
        }
    }

    @OnFocusChange({R.id.tv_jdsj, R.id.tv_dcrq})
    public void onFocusChange(View v, boolean hasFocus) {
        DatePickerDialog dialog = null;
        if (!hasFocus) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_jdsj:
                dialog = new DatePickerDialog(PuchaLuruActivity.this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {

                        //当前选择的日期
                        String date = year + "-" + TimeUtils.getOrderNum(Integer.parseInt(monthOfYear + 1 + "")) + "-" + TimeUtils.getOrderNum(dayOfMonth) + " ";

                        TimePickerDialog dialog = new TimePickerDialog(PuchaLuruActivity.this, new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                                String time = TimeUtils.getOrderNum(hourOfDay) + ":" + TimeUtils.getOrderNum(minute) + ":" + "00";
                                tvJdsj.setText(date + time);

                            }
                        }, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), true);

                        //显示
                        dialog.show();

                    }
                }, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
                //通过Calendar获得当前年、月、日

                //显示
                dialog.show();
                break;
            case R.id.tv_dcrq:
                dialog = new DatePickerDialog(PuchaLuruActivity.this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {

                        //当前选择的日期
                        String date = year + "-" + TimeUtils.getOrderNum(Integer.parseInt(monthOfYear + 1 + "")) + "-" + TimeUtils.getOrderNum(dayOfMonth) + " ";

                        TimePickerDialog dialog = new TimePickerDialog(PuchaLuruActivity.this, new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                                String time = TimeUtils.getOrderNum(hourOfDay) + ":" + TimeUtils.getOrderNum(minute) + ":" + "00";
                                tvDcrq.setText(date + time);

                            }
                        }, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), true);

                        //显示
                        dialog.show();

                    }
                }, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
                //通过Calendar获得当前年、月、日

                //显示
                dialog.show();
                break;
            default:
                break;
        }
    }
}
